package org.eclipse.snippets.code;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.StringReader;
import java.util.HashMap;

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CTabFolder;
import org.eclipse.swt.custom.CTabItem;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.ToolBar;
import org.eclipse.swt.widgets.ToolItem;

public class CsvReader {
	private Display display;
	private Shell shell;
	private HashMap<String, Image> hashImages;
	private Label label;
	private Table table;
	private Text source;

	private final String initialCSV = "Col #1;Col #2;Col #3\nA;a;0\nB;b;1\nC;c;2\n";
	private Color GRAY;
	private Color WHITE;
	private CTabFolder tab;

	public CsvReader() {
		display = new Display();
		shell = new Shell(display);

		init();
		createGUI();

		shell.open();

		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
	}

	private void init() {
		hashImages = new HashMap<String, Image>();
		// TODO remove absolute path
		hashImages.put("open", new Image(display, "image/document-new.png"));
		hashImages.put("close", new Image(display, "image/document-open.png"));
		hashImages.put("save", new Image(display, "image/document-save.png"));
		hashImages.put("saveas", new Image(display, "image/print.png"));
		hashImages.put("exit", new Image(display, "image/edit-undo.png"));

		hashImages.put("cut", new Image(display, "image/edit-redo.png"));
		hashImages.put("copy", new Image(display, "image/edit-cut.png"));
		hashImages.put("paste", new Image(display, "image/copy.png"));

		hashImages.put("undo", new Image(display, "image/edit-paste.png"));
		hashImages.put("redo", new Image(display, "image/search.png"));

		// hashImages.put("help", new Image(display,
		// "C:\\Programme\\eclipse\\workspace\\TestSwt\\src\\icons\\help.png"));
		// hashImages.put("about", new Image(display,
		// "C:\\Programme\\eclipse\\workspace\\TestSwt\\src\\icons\\about_kde.png"));
		//
		// hashImages.put("tab_table", new Image(display,
		// "C:\\Programme\\eclipse\\workspace\\TestSwt\\src\\icons\\1day.png"));
		// hashImages.put("tab_source", new Image(display,
		// "C:\\Programme\\eclipse\\workspace\\TestSwt\\src\\icons\\14_pencil.png"));
		// hashImages.put("reload", new Image(display,
		// "C:\\Programme\\eclipse\\workspace\\TestSwt\\src\\icons\\reload.png"));

		GRAY = new Color(display, 220, 220, 220);
		WHITE = new Color(display, 255, 255, 255);
	}

	private void createGUI() {
		shell.setLayout(new GridLayout(1, false));
		shell.setText("CSV Reader");
		shell.setSize(500, 300);

		shell.setImage(hashImages.get("about"));

		ToolBar bar = new ToolBar(shell, SWT.NONE);
		bar.setLayoutData(new GridData());
		ToolItem citem = new ToolItem(bar, SWT.NONE);
		// citem.setText("Reload");
		citem.setToolTipText("Reload table");
		citem.setImage(hashImages.get("reload"));
		citem.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				reloadData();
				label.setText("Table reloaded.");
			}
		});

		tab = new CTabFolder(shell, SWT.CLOSE);
		tab.setLayoutData(new GridData(GridData.FILL_BOTH
				| GridData.GRAB_HORIZONTAL | GridData.GRAB_VERTICAL));

		CTabItem item = new CTabItem(tab, SWT.NONE);
		// table = new Table(tab, SWT.SINGLE | SWT.FULL_SELECTION | SWT.V_SCROLL
		// | SWT.H_SCROLL);
		// table.setHeaderVisible(true);
		// table.setLinesVisible(true);

		// item.setText("CSV Data");
		// item.setImage(hashImages.get("tab_table"));
		// item.setControl(table);

		item = new CTabItem(tab, SWT.NONE);
		source = new Text(tab, SWT.V_SCROLL | SWT.H_SCROLL);
		source.setText(initialCSV);
		item.setText("CSV Source");
		item.setImage(hashImages.get("tab_source"));
		item.setControl(source);

		// label = new Label(shell, SWT.NONE);
		// label.setLayoutData(new GridData(GridData.FILL_HORIZONTAL |
		// GridData.GRAB_HORIZONTAL));
		// label.setText("Ready");
	}

	private void reloadData() {
		System.err.println("reloading");
		BufferedReader br = new BufferedReader(new StringReader(
				source.getText()));
		String line = null;
		String[] linedata;
		try {
			// read first line with headers
			line = br.readLine();
			linedata = line.split(";");
			// System.err.println("got headers '" + line + "' (" +
			// linedata.length + ")");
			TableColumn col;
			for (int i = 0; i < linedata.length; i++) {
				col = new TableColumn(table, SWT.LEFT, i);
				col.setText(linedata[i]);
				col.setWidth(100);
			}

			TableItem row;
			int count = 0;
			while ((line = br.readLine()) != null) {
				linedata = line.split(";");
				// System.err.println("adding '" + line + "' (" +
				// linedata.length + ")");
				row = new TableItem(table, SWT.NONE);
				row.setText(linedata);
				row.setBackground((count++ % 2 == 0) ? GRAY : WHITE);
			}
			br.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public static void main(String[] args) {
		new CsvReader();
	}
}
